<?php
/*
 * Copyright (c) 2011 Todd Brady
 *
 * PS3Theatre is freely distributable under the terms of the MIT license.
 * For details, see the MIT-Liscence web site: http://www.opensource.org/licenses/mit-license.php 
 */
 	include("includes/db_connect.php");
	
	$message = $_GET['message'];
	$moviesListResult=mysql_query("SELECT m.id, m.title, m.released_date, d.label as disc_src, a.label as audio_src, m.is_watched FROM tbl_movies m LEFT OUTER JOIN tbl_disc_flags d ON (d.id=m.disc_flag_id) LEFT OUTER JOIN tbl_audio_flags a ON (a.id=m.audio_flag_id) ORDER BY m.title");
	
	//fill 2 dropdowns
	$audioFlagQuery =  "SELECT id,label from tbl_audio_flags ORDER BY label";
	$discFlagQuery = "SELECT id,label from tbl_disc_flags ORDER BY label";
	$audioFlags=mysql_query($audioFlagQuery) or die(mysql_error());
	$discFlags=mysql_query($discFlagQuery) or die(mysql_error());
	
	if(isset($_POST['action']))
	{
		$updateQuery = "update tbl_movies SET";
		$changeVideoSrc = $_POST['changeVideoSrc'];
		$discFlagId = $_POST['discFlagId'];
		$changeAudioSrc = $_POST['changeAudioSrc'];
		$audioFlagId = $_POST['audioFlagId'];
		$changeWatched = $_POST['changeWatched'];
		$isWatchedFlag = $_POST['isWatchedFlag'];
		$movieIdList = $_POST['movieIdList'];
		
		$i=0;
		$update = (($changeVideoSrc == 'Y' || $changeAudioSrc == 'Y' || $changeWatched == 'Y') && count($movieIdList)>0);

		$message = "";
		
		if($update){
			if($changeVideoSrc == 'Y'){
				if($i != 0){
					$updateQuery.=', ';
				}
				
				$updateQuery.=' disc_flag_id='.$discFlagId;
				$i++;
			}
			
			if($changeAudioSrc == 'Y'){
				if($i != 0){
					$updateQuery.=', ';
				}
				
				$updateQuery.=' audio_flag_id='.$audioFlagId;
				$i++;
			}
			
			if($changeWatched == 'Y'){
				if($i != 0){
					$updateQuery.=', ';
				}
				
				$updateQuery.=" is_watched='".$isWatchedFlag."'";
				$i++;
			}
			
			$updateQuery.=' WHERE id in (';
			
			for($j=0; $j<count($movieIdList); $j++){
				if($j != 0){
					$updateQuery.=', ';
				}		
				
				$updateQuery.=$movieIdList[$j];		
			}
			
			$updateQuery.=');';
			$update=mysql_query($updateQuery) or die('error while trying to update: '.mysql_error());
			$message = "Movies successfully updated.";
			
			//redirect to movie setup with message
			header("Location: movies-editMany.php?message=".$message);
		}else{
			$message = "No movies selected, or no fields selected for update.";
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<LINK href="../css/main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../js/jQuery.js"></script>
<script type="text/javascript" src="../js/simpleModal.js"></script>
<script>
function checkAll(){
	var chk = document.editMany["movieIdList[]"];
	
	for (i = 0; i < chk.length; i++)
		chk[i].checked = true;
}

function uncheckAll(){
	var chk = document.editMany["movieIdList[]"];
	
	for (i = 0; i < chk.length; i++)
		chk[i].checked = false;
}

function videoSrcChanged(){
	if(document.editMany.changeVideoSrc.checked){
		document.editMany.discFlagId.disabled = false;
	}else{
		document.editMany.discFlagId.disabled = true;
	}
}

function audioSrcChanged(){
	if(document.editMany.changeAudioSrc.checked){
		document.editMany.audioFlagId.disabled = false;
	}else{
		document.editMany.audioFlagId.disabled = true;
	}
}

function watchedChanged(){
	if(document.editMany.changeWatched.checked){
		document.editMany.isWatchedFlag.disabled = false;
	}else{
		document.editMany.isWatchedFlag.disabled = true;
	}
}
</script>
</head>
<body>
<?php echo $message; ?>
<h4>Movies Edit Many</h4>
<form name="editMany" method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input name="action" type="hidden" id="action" value="editMany" />
<table cellpadding="2" class="rowmajor">
 <tbody>
   <tr>
     <th class="vt"><label>
       <input type="checkbox" name="changeVideoSrc" id="changeVideoSrc" value="Y" onchange="javascript:videoSrcChanged();"/>
     </label></th>
     <th class="vt"><div align="left">Video Src: </div></th>
     <td><label>
       <select name="discFlagId" id="discFlagId" disabled="disabled">
	   <option value="" >--None--</option>
	  <?php 
	  	while ($row = mysql_fetch_array($discFlags)) {
	 ?>
	 	<option value="<?php echo $row['id'];?>" <?php if($discFlagId == $row['id']){ echo 'selected="selected"';}?>><?php echo $row['label'];?></option>
	 <?php
	 }
	 ?>
       </select>
     </label></td>
     </tr>
   <tr>
     <th class="vt"><label>
       <input type="checkbox" name="changeAudioSrc" value="Y" onchange="javascript:audioSrcChanged();" />
     </label></th>
     <th class="vt"><div align="left">Audio Src:</div></th>
     <td><select name="audioFlagId" id="audioFlagId" disabled="disabled">
	 		<option value="">--None--</option>
       <?php 
	  	while ($row = mysql_fetch_array($audioFlags)) {
	 ?>
       <option value="<?php echo $row['id'];?>" <?php if($audioFlagId == $row['id']){ echo 'selected="selected"';}?>><?php echo $row['label'];?></option>
       <?php
	 }
	 ?>
     </select></td>
   </tr>
   <tr>
     <th class="vt"><label>
       <input type="checkbox" name="changeWatched" value="Y" onchange="javascript:watchedChanged();" />
     </label></th>
      <th class="vt"><div align="left">Watched:</div></th>
     <td><select name="isWatchedFlag" id="isWatchedFlag" disabled="disabled">
	 	<option value="N">No</option>
       	<option value="Y">Yes</option>
     </select></td>
   </tr>
 </tbody></table>
<p>
  <label>
  <input type="submit" name="Submit" value="Save" />
  </label>
  <input type="button" name="cancelButton" value="Cancel" onclick="location='movies-setup.php'"/>
</p>

<table width="98%" border="0" cellspacing="0" cellpadding="0" class="setupTable">
  <tr>
  <th colspan="6">
    <input type="button" value="Check All" onclick="javascript:checkAll();" />
    <input type="button" value="Uncheck All" onclick="javascript:uncheckAll();"/>  </th>
  <tr>
    <th width="2%">&nbsp;</th>
    <th width="34%">Title</th>
    <th width="14%">Rel Date </th>
    <th width="23%">Video Src</th>
    <th width="23%">Audio Src</th>
	<th width="4%">Watched</th>
  </tr>
  <?php
    $i=0;
	while ($row = mysql_fetch_array($moviesListResult)) {
	$i++;
	$class = "";
	
	if($i % 2 == 0){
		$class = 'class="alt"';
	}
  ?>
  <tr>
    <td <?php echo $class;?>><input type="checkbox" id="movieIdList[]" name="movieIdList[]" value="<?php echo $row['id'];?>" /></td>
    <td <?php echo $class;?>><?php echo $row['title'];?></td>
    <td <?php echo $class;?>><?php echo $row['released_date'];?></td>
    <td <?php echo $class;?>><?php echo $row['disc_src'];?></td>
    <td <?php echo $class;?>><?php echo $row['audio_src'];?></td>
    <td <?php echo $class;?>><?php echo $row['is_watched'];?></td>
  </tr>
   <?php 
   }
   
  	if(mysql_num_rows($moviesListResult) == 0){
  ?>
  <tr>
    <td colspan="6"><div align="center">No movies in library </div></td>
  </tr>
  <?php
  }
  ?>
  <tr>
    <td class="footer" colspan="6">
		<input type="button" value="Check All" onclick="javascript:checkAll();"/>
    	<input type="button" value="Uncheck All"onclick="javascript:uncheckAll();" />
	</td>
  </tr>
</table>

</form>
</body>
</html>
